/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pheelicks.app.utils;

import ohos.media.audio.AudioRenderer;
import ohos.media.audio.AudioRendererInfo;
import ohos.media.audio.AudioStreamInfo;

import java.io.InputStream;
import java.util.Locale;

/**
 * AudioRenderer播放工具类
 *
 * @since 2021-05-08
 */
public class PlaySoundUtil {
    private AudioStreamInfo audioStreamInfo = null;
    private AudioRendererInfo audioRendererInfo = null;
    private AudioRenderer.PlayMode playMode = AudioRenderer.PlayMode.MODE_STREAM;
    private AudioRenderer audioRenderer = null;
    private InputStream soundInputStream = null;

    public PlaySoundUtil() {
        audioStreamInfo = new AudioStreamInfo.Builder()
            .sampleRate(44100) // 44.1kHz
            .audioStreamFlag(AudioStreamInfo.AudioStreamFlag.AUDIO_STREAM_FLAG_MAY_DUCK) // 混音
            .encodingFormat(AudioStreamInfo.EncodingFormat.ENCODING_PCM_16BIT) // 16-bit PCM
            .channelMask(AudioStreamInfo.ChannelMask.CHANNEL_OUT_STEREO) // 双声道输出
            .streamUsage(AudioStreamInfo.StreamUsage.STREAM_USAGE_MEDIA) // 媒体类音频
            .build();

        audioRendererInfo = new AudioRendererInfo.Builder().audioStreamInfo(audioStreamInfo)
            .audioStreamOutputFlag(AudioRendererInfo.AudioStreamOutputFlag.AUDIO_STREAM_OUTPUT_FLAG_DIRECT_PCM) // pcm格式的输出流
            .bufferSizeInBytes(1024)
            .isOffload(false) // false表示分段传输buffer并播放，true表示整个音频流一次性传输到HAL层播放
            .build();
        audioRenderer = new AudioRenderer(audioRendererInfo, playMode);
    }

    /**
     * 设置rawfile文件下的类型为wav的文件名
     *
     * @param fileName 文件名
     */
    public void loadSound(String fileName) {
        String filePath = String.format(Locale.ROOT, "assets/entry/resources/rawfile/%s", fileName);
        soundInputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
    }

    /**
     * 开始播放
     */
    public void start() {
        byte[] buffer = new byte[1024];
        int len;

        try {
            audioRenderer.start();
            while ((len = soundInputStream.read(buffer, 0, buffer.length)) != -1) {
                audioRenderer.write(buffer, 0, buffer.length);
            }
            soundInputStream.available();
            soundInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取AudioRenderer
     *
     * @return AudioRenderer
     */
    public AudioRenderer getAudioRenderer() {
        return this.audioRenderer;
    }
}
